﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html;
using System.IO;
using System.Web;
using negocios;
using objetos;

namespace relatorios
{



    public class REL_PDFPAGE : iTextSharp.text.pdf.PdfPageEventHelper
    {

        //I create a font object to use within my footer

        public int relatorio_tipo { get; set; }
        public float margem_superior { get; set; }
        public float margem_inferior { get; set; }
        public float margem_esquerda { get; set; }
        public float margem_direita { get; set; }
        public int tamanho_titulo { get; set; }
        public int tamanho_campo { get; set; }
        public int tamanho_rodape { get; set; }
        public int tamanho_texto { get;set;} 


        private enum Fonte
        {
            Arial = 1,
            Verdana = 2,
            Helvetica = 3
        }

        //override the OnStartPage event handler to add our header

        public override void OnStartPage(PdfWriter writer, Document doc)
        {



            //I use a PdfPtable with 1 column to position my header where I want it

            PdfPTable headerTbl = new PdfPTable(1);

            //set the width of the table to be the same as the document

            headerTbl.TotalWidth = doc.PageSize.Width;

            //Add the cell to the table

            headerTbl.AddCell(Logotipo());

            //write the rows out to the PDF output stream. I use the height of the document to position the table. Positioning seems quite strange in iTextSharp and caused me the biggest headache.. It almost seems like it starts from the bottom of the page and works up to the top, so you may ned to play around with this.

            headerTbl.WriteSelectedRows(0, -1, 0, (doc.PageSize.Height - 10), writer.DirectContent);

        }

        //override the OnPageEnd event handler to add our footer

        public override void OnEndPage(PdfWriter writer, Document doc)
        {

            //I use a PdfPtable with 2 columns to position my footer where I want it

            PdfPTable footerTbl = new PdfPTable(2);

            float[] widths = new float[] { 1f, 1f };

            footerTbl.SetWidths(widths);

            //  Font smallfont = FontFactory.GetFont("Arial", 7);

            //set the width of the table to be the same as the document

            footerTbl.TotalWidth = doc.PageSize.Width;

            //Center the table on the page

            footerTbl.HorizontalAlignment = Element.ALIGN_CENTER;

            PdfPCell cellData = new PdfPCell(Texto(DateTime.Now.ToShortDateString(), 7));

            cellData.Border = 0;

            cellData.HorizontalAlignment = 0; //0=Left, 1=Centre, 2=Right

            cellData.PaddingLeft = doc.LeftMargin;

            PdfPCell cellPagina = new PdfPCell(Texto("Página " + doc.PageNumber.ToString(), 7));

            cellPagina.Border = 0;

            cellPagina.HorizontalAlignment = 2; //0=Left, 1=Centre, 2=Right

            cellPagina.PaddingRight = doc.RightMargin;

            footerTbl.AddCell(cellData);

            footerTbl.AddCell(cellPagina);

            //write the rows out to the PDF output stream.

            footerTbl.WriteSelectedRows(0, -1, 0, (doc.BottomMargin + 5), writer.DirectContent);

        }



        public bool showPageCount { get; set; }



        public Font FonteArial(int tamanho)
        {

            Font arial = FontFactory.GetFont("Arial", tamanho, BaseColor.BLACK);

            return arial;

        }

        public Font FonteVerdana(int tamanho)
        {

            Font arial = FontFactory.GetFont("Verdana", tamanho, BaseColor.BLACK);

            return arial;

        }

        public Font FontHelvetica(int tamanho)
        {

            Font helvetica = FontFactory.GetFont("Helvetica", tamanho, BaseColor.BLACK);

            return helvetica;

        }

        public Paragraph QuebraLinha()
        {

            Phrase ps = new Phrase("\n");

            Paragraph p = new Paragraph(ps);

            return p;

        }

        public Paragraph Texto(String texto, int tamanho)
        {

            Phrase ps = new Phrase(texto, FonteArial(tamanho));

            Paragraph p = new Paragraph(ps);

            return p;

        }

        public Paragraph Texto(String texto, int tamanho, int alinhamento)
        {

            Phrase ps = new Phrase(texto, FonteArial(tamanho));

            Paragraph p = new Paragraph(ps);

            switch (alinhamento)
            {

                case 0:

                    p.Alignment = Element.ALIGN_LEFT;

                    break;

                case 1:

                    p.Alignment = Element.ALIGN_CENTER;

                    break;

                case 2:

                    p.Alignment = Element.ALIGN_RIGHT;

                    break;

            }

            return p;

        }

        public Paragraph Titulo(String texto, int tamanho)
        {

            Phrase ps = new Phrase(texto, FonteArial(tamanho));

            Paragraph p = new Paragraph(ps);

            return p;

        }

        /// <summary>

        /// 0 - ESQUERDA , 1 - CENTRALIZADO , 2 - DIREITA

        /// </summary>

        /// <param name="texto"></param>

        /// <param name="tamanho"></param>

        /// <param name="alinhamento"></param>

        /// <returns></returns>

        public Paragraph Titulo(String texto, int tamanho, int alinhamento)
        {

            Phrase ps = new Phrase(texto, FonteArial(tamanho));

            Paragraph p = new Paragraph(ps);

            switch (alinhamento)
            {

                case 0:

                    p.Alignment = Element.ALIGN_LEFT;

                    break;

                case 1:

                    p.Alignment = Element.ALIGN_CENTER;

                    break;

                case 2:

                    p.Alignment = Element.ALIGN_RIGHT;

                    break;

            }

            return p;

        }

        public void Download(string file)
        {

            HttpContext.Current.Response.ClearContent();
            HttpContext.Current.Response.ClearHeaders();
            HttpContext.Current.Response.ContentType = "application/pdf";
            HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(file));
            HttpContext.Current.Response.TransmitFile(@file);                        
            HttpContext.Current.Response.Flush();
            HttpContext.Current.Response.Clear();

        }

        public PdfPCell Logotipo()
        {

            String imglogo = HttpContext.Current.Server.MapPath("~/images/logo");
            Image logo = Image.GetInstance(imglogo + "/logo.png");
            logo.ScalePercent(30f);
            PdfPCell chLogo = new PdfPCell(logo);
            chLogo.Border = 0;

            //add a bit of padding to bring it away from the right edge
            chLogo.Padding = 20;
            chLogo.HorizontalAlignment = Element.ALIGN_RIGHT;
            return chLogo;

        }



        public PdfPTable Tabela(int colunas)
        {
            PdfPTable tb = new PdfPTable(colunas);
            return tb;
        }



        public PdfPTable Tabela(PdfPTable tabela)
        {
            PdfPTable tb = new PdfPTable(tabela);
            return tb;
        }



        public PdfPCell Celula(string texto, int tamanho)
        {

            PdfPCell cell = new PdfPCell(new Phrase(texto, FonteArial(tamanho)));
            return cell;

        }

        public PdfPCell Celula(Image img)
        {

            PdfPCell cell = new PdfPCell(img);
            return cell;

        }

    }

}